package com.ymatou.shop.ui.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.View;

import com.ymatou.shop.R;
import com.ymatou.shop.YmatouApplication;
import com.ymatou.shop.account.Account;
import com.ymatou.shop.api.ApiResponse;
import com.ymatou.shop.cache.CacheManager;
import com.ymatou.shop.cache.OnCacheListener;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.cache.exception.CacheException;
import com.ymatou.shop.model.ResponseMessage;
import com.ymatou.shop.util.DialogUtil;
import com.ymatou.shop.util.GlobalUtil;
import com.ymatou.shop.util.PreferenceManager;

public abstract class BaseActivity extends FragmentActivity implements OnCacheListener {

	protected View mLoadingView;
	private View mErrorView;

	private PreferenceManager mPreferenceManager;
	private CacheManager mCacheManager;
	private Account mAccount;
	private ProgressDialog mProgressDialog;
	private static Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mAccount = Account.getInstance(getApplication());
		mCacheManager = CacheManager.getInstance(getApplication());
		mPreferenceManager = PreferenceManager.getInstance(getApplicationContext());

		mProgressDialog = new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setMessage(getString(R.string.login_ing_text));
	}

	private Handler getHandler() {
		if (mHandler == null) {
			mHandler = new Handler();
		}
		return mHandler;
	}

	protected void showProgressDialog() {
		getHandler().post(new Runnable() {

			@Override
			public void run() {
				if (mProgressDialog != null) {
					mProgressDialog.show();
				}
			}
		});

	}

	protected void setMeesage(int resId) {
		mProgressDialog.setMessage(getString(resId));
	}

	protected void setMeesage(String string) {
		mProgressDialog.setMessage(string);
	}

	protected void cancelProgressDialog() {
		getHandler().post(new Runnable() {

			@Override
			public void run() {
				if (mProgressDialog != null) {
					mProgressDialog.cancel();
				}
			}
		});
	}

	protected void setProgressDialogMessage(int resId) {
		mProgressDialog.setMessage(getString(resId));
	}

	protected void setupViews() {
		mLoadingView = findViewById(R.id.fullscreen_loading_root);
	}

	protected void setupData(boolean useCache) {
		// none
	}

	public CacheManager getCacheManager() {
		return mCacheManager;
	}

	public PreferenceManager getPreferenceManager() {
		return mPreferenceManager;
	}

	public Account getAccount() {
		return mAccount;
	}

	public YmatouApplication getYmatouApplication() {
		return (YmatouApplication) getApplication();
	}

	protected void changeLoadingViewState(boolean hideLoading) {
		if (hideLoading) {
			changeViewState(View.GONE, mLoadingView);
			changeViewState(View.VISIBLE, getContentViews());
		} else {
			changeViewState(View.VISIBLE, mLoadingView);
			changeViewState(View.GONE, getContentViews());
		}
	}

	protected void changeErrorViewState(boolean hideError) {
		if (hideError) {
			changeViewState(View.GONE, mErrorView);
			changeViewState(View.GONE, mLoadingView);
			changeViewState(View.VISIBLE, getContentViews());
		} else {
			changeViewState(View.VISIBLE, mErrorView);
			changeViewState(View.GONE, mLoadingView);
			changeViewState(View.GONE, getContentViews());
		}
	}

	public void changeViewState(int visibility, View... views) {
		if (views == null)
			return;

		for (View v : views) {
			if (v != null)
				v.setVisibility(visibility);
		}
	}

	public View[] getContentViews() {
		return null;
	}

	@Override
	protected void onResume() {
		super.onResume();
		//MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		//MobclickAgent.onPause(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mCacheManager.unregister(this);
	}

	public void startLoginActivity() {
		GlobalUtil.startActivity(this, LoginActivity.class);
	}

	@Override
	public void onCacheSuccess(int requestId, RequestInfo requestInfo, Object response) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onCacheFailed(int requestId, RequestInfo requestInfo, CacheException error) {
		ResponseMessage responseMessage = ApiResponse.invitationCode((String) error.getOrgResponse());
		if (responseMessage != null){
			DialogUtil.alertDialog(this, responseMessage.message);	
		}
	}
}
